package com.tebiecloud.mpopen.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.tebiecloud.mpopen.client.model.entity.MpopenAuthorizerInfo;
import com.tebiecloud.mpopen.server.service.MpopenAuthorizerInfoService;
import com.tebiecloud.common.model.PageParams;
import com.tebiecloud.common.model.ResultBody;
import com.tebiecloud.common.utils.ResultBodyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import java.util.Arrays;
import java.util.Map;

/**
 * 公众号的帐号基本信息 前端控制器
 * @author tebie
 * @date 2019-09-19
 */
@RestController
@RequestMapping("/mpopenauthorizerinfo")
public class MpopenAuthorizerInfoController {

    @Autowired
    private MpopenAuthorizerInfoService mpopenAuthorizerInfoService;

    /**
     * 获取分页数据
     * @param map
     * @return
     */
    @GetMapping(value = "list")
    public ResultBody<IPage<MpopenAuthorizerInfo>> list(@RequestParam(required = false) Map map) {
        PageParams pageParams = new PageParams(map);
        MpopenAuthorizerInfo query = pageParams.mapToObject(MpopenAuthorizerInfo.class);
        QueryWrapper<MpopenAuthorizerInfo> queryWrapper = new QueryWrapper();
        return ResultBodyUtils.ok(mpopenAuthorizerInfoService.page(new PageParams(map), queryWrapper));
    }

    /**
     * 根据ID查找数据
     * @param id
     * @return
     */
    @GetMapping("get")
    public ResultBody<MpopenAuthorizerInfo> get(@RequestParam("id") Long id) {
        MpopenAuthorizerInfo entity = mpopenAuthorizerInfoService.getById(id);
        return ResultBodyUtils.ok(entity);
    }

    /**
     * 添加数据
     * @param nickName 授权方头像,
     * @param headImg 授权方头像,
     * @param serviceTypeInfo 授权方公众号类型，0代表订阅号，1代表由历史老帐号升级后的订阅号，2代表服务号,
     * @param verifyTypeinfo 授权方认证类型，-1代表未认证，0代表微信认证，1代表新浪微博认证，2代表腾讯微博认证，3代表已资质认证通过但还未通过名称认证，4代表已资质认证通过、还未通过名称认证，但通过了新浪微博认证，5代表已资质认证通过、还未通过名称认证，但通过了腾讯微博认证,
     * @param userName 授权方公众号的原始ID,
     * @param principalName 公众号的主体名称,
     * @param alias 授权方公众号所设置的微信号，可能为空,
     * @param businessInfo 用以了解功能的开通状况（0代表未开通，1代表已开通）,
     * @param qrcodeUrl 二维码图片的URL，开发者最好自行也进行保存,
     * @param appId 授权方appid,
     * @param funcInfo 公众号授权给开发者的权限集列表
     * @return
     */
    @PostMapping("add")
    public ResultBody add(
        @RequestParam(value = "nickName", required = false) String nickName,
        @RequestParam(value = "headImg", required = false) String headImg,
        @RequestParam(value = "serviceTypeInfo", required = false) String serviceTypeInfo,
        @RequestParam(value = "verifyTypeinfo", required = false) String verifyTypeinfo,
        @RequestParam(value = "userName", required = false) String userName,
        @RequestParam(value = "principalName", required = false) String principalName,
        @RequestParam(value = "alias", required = false) String alias,
        @RequestParam(value = "businessInfo", required = false) String businessInfo,
        @RequestParam(value = "qrcodeUrl", required = false) String qrcodeUrl,
        @RequestParam(value = "appId", required = false) String appId,
        @RequestParam(value = "funcInfo", required = false) String funcInfo
    ) {
        MpopenAuthorizerInfo entity = new MpopenAuthorizerInfo();
        entity.setNickName(nickName);
        entity.setHeadImg(headImg);
        entity.setServiceTypeInfo(serviceTypeInfo);
        entity.setVerifyTypeinfo(verifyTypeinfo);
        entity.setUserName(userName);
        entity.setPrincipalName(principalName);
        entity.setAlias(alias);
        entity.setBusinessInfo(businessInfo);
        entity.setQrcodeUrl(qrcodeUrl);
        entity.setAppId(appId);
        entity.setFuncInfo(funcInfo);
        mpopenAuthorizerInfoService.save(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 更新数据
     * @param nickName 授权方头像
     * @param headImg 授权方头像
     * @param serviceTypeInfo 授权方公众号类型，0代表订阅号，1代表由历史老帐号升级后的订阅号，2代表服务号
     * @param verifyTypeinfo 授权方认证类型，-1代表未认证，0代表微信认证，1代表新浪微博认证，2代表腾讯微博认证，3代表已资质认证通过但还未通过名称认证，4代表已资质认证通过、还未通过名称认证，但通过了新浪微博认证，5代表已资质认证通过、还未通过名称认证，但通过了腾讯微博认证
     * @param userName 授权方公众号的原始ID
     * @param principalName 公众号的主体名称
     * @param alias 授权方公众号所设置的微信号，可能为空
     * @param businessInfo 用以了解功能的开通状况（0代表未开通，1代表已开通）
     * @param qrcodeUrl 二维码图片的URL，开发者最好自行也进行保存
     * @param appId 授权方appid
     * @param funcInfo 公众号授权给开发者的权限集列表
     * @return
     */
    @PostMapping("update")
    public ResultBody update(
            @RequestParam(value = "authorizerInfoId", required = false) Long authorizerInfoId,
            @RequestParam(value = "nickName", required = false) String nickName,
            @RequestParam(value = "headImg", required = false) String headImg,
            @RequestParam(value = "serviceTypeInfo", required = false) String serviceTypeInfo,
            @RequestParam(value = "verifyTypeinfo", required = false) String verifyTypeinfo,
            @RequestParam(value = "userName", required = false) String userName,
            @RequestParam(value = "principalName", required = false) String principalName,
            @RequestParam(value = "alias", required = false) String alias,
            @RequestParam(value = "businessInfo", required = false) String businessInfo,
            @RequestParam(value = "qrcodeUrl", required = false) String qrcodeUrl,
            @RequestParam(value = "appId", required = false) String appId,
            @RequestParam(value = "funcInfo", required = false) String funcInfo
    ) {
        MpopenAuthorizerInfo entity = new MpopenAuthorizerInfo();
        entity.setAuthorizerInfoId(authorizerInfoId);
        entity.setNickName(nickName);
        entity.setHeadImg(headImg);
        entity.setServiceTypeInfo(serviceTypeInfo);
        entity.setVerifyTypeinfo(verifyTypeinfo);
        entity.setUserName(userName);
        entity.setPrincipalName(principalName);
        entity.setAlias(alias);
        entity.setBusinessInfo(businessInfo);
        entity.setQrcodeUrl(qrcodeUrl);
        entity.setAppId(appId);
        entity.setFuncInfo(funcInfo);
        mpopenAuthorizerInfoService.updateById(entity);
        return ResultBodyUtils.ok();
    }

    /**
     * 删除数据
     * @param id 单个id
     * @param ids 多个id用,号隔开
     * @return
     */
    @PostMapping("remove")
    public ResultBody remove(
            @RequestParam(value = "id", required = false) Long id,
            @RequestParam(value = "ids", required = false) String ids
    ) {
        if(id != null && id > 0) {
            mpopenAuthorizerInfoService.removeById(id);
        }
        if(ids != null && ids.length() > 0) {
            mpopenAuthorizerInfoService.removeByIds(Arrays.asList(ids.split(",")));
        }
        return ResultBodyUtils.ok();
    }

}
